package com.tcwlkj.uuService.jpush;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.widget.Toast;

import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SynthesizerListener;
import com.tcwlkj.uuService.MainActivity;
import com.tcwlkj.uuService.MyApp;
import com.tcwlkj.uuService.enums.CheckStatus;
import com.tcwlkj.uuService.model.MainMessage;
import com.tcwlkj.uuService.model.UpdateMessage;
import com.tcwlkj.uuService.utils.EventCenter;
import com.tcwlkj.uuService.utils.PreferencesUtils;
import com.tcwlkj.uuService.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

import static cn.jpush.android.api.JPushInterface.ACTION_NOTIFICATION_RECEIVED;
import static cn.jpush.android.api.JPushInterface.EXTRA_EXTRA;

/**
 * Created by Administrator on 2017/10/14 0014.
 */

public class MyJpushReceiver extends BroadcastReceiver {

    private Bundle bundle;
    private JSONObject json;
    private UpdateMessage updateMessage;
    private Intent i;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onReceive(Context context, Intent intent) {
        bundle = intent.getExtras();
        if(JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())){//接受到推送下来的自定义消息
            try {
                json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                if(Integer.parseInt(json.getString("type"))==0){//订单推送、
                    updateMessage = new UpdateMessage(json.getString("OrderDetail"), json.getString("notifybody"));
                    updateMessage.setType(json.getString("type"));
                    EventBus.getDefault().post(new EventCenter<UpdateMessage>(55, updateMessage));
                }
                if(Integer.parseInt(json.getString("type"))==1){//指派订单,可抢
                    updateMessage=new UpdateMessage(json.getString("OrderDetail"), json.getString("notifybody"));
                    updateMessage.setType(json.getString("type"));
                    EventBus.getDefault().post(new EventCenter<UpdateMessage>(55,updateMessage));
                }
                if(Integer.parseInt(json.getString("type"))==2){//强制指派
                    i = new Intent(context, MainActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    i.putExtra("NotifyID","-55");
                    i.putExtra("notifybody", json.getString("notifybody"));
                    context.startActivity(i);
                }
                if(Integer.parseInt(json.getString("type"))==CheckStatus.Customer.getKey()){
                    EventBus.getDefault().post(new EventCenter<String>(CheckStatus.Customer.getKey(), json.getString("notifybody")));
                }
                if(Integer.parseInt(json.getString("type"))==50){//身份认证成功
                    EventBus.getDefault().post( new EventCenter<String>(50, json.getString("notifybody")));
                }
                if(Integer.parseInt(json.getString("type"))==199){//异地登录
                    EventBus.getDefault().post(new EventCenter<String>(40111,"退出登录"));
                }
                if(Integer.parseInt(json.getString("type"))==14){
                    i=new Intent(context, MainActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    i.putExtra("NotifyID","-12");
                    context.startActivity(i);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }else if(JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())){//接受到推送下来的通知
            Log.d("inform","通知");
        }else if(JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())){//用户点击打开了通知
            try {
                JSONObject jsons = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                if(jsons.getString("type").equals("0")||jsons.getString("type").equals("1")){//订单推送和普通指派
                    i=new Intent(context, MainActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    i.putExtra("OrderDetail",jsons.getString("OrderDetail"));
                    i.putExtra("type",jsons.getString("type"));
                    i.putExtra("NotifyID","1");
                    context.startActivity(i);
                }else if(jsons.getString("type").equals("2")) {//强制指派
                    i=new Intent(context, MainActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    context.startActivity(i);
                }else {//普通通知点击
                    i=new Intent(context, MainActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    context.startActivity(i);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }else if(JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())){//用户注册成功
        }
    }

}
